package com.ly.datacenter.database.definition;

import java.util.ArrayList;
import java.util.List;

import com.ly.datacenter.crud.json.CRUDField;
import com.ly.datacenter.database.pojo.FieldMetadata;
import com.ly.datacenter.jdbc.util.DAOUtil;
import com.ly.datacenter.jdbc.util.SQLTypeParserUtil;
import com.ly.datacenter.pojo.KeyValue;
import com.ly.datacenter.pojo.KeyValueList;

/**
 * function：definition of table in table, use for CRUDing entity
 * 
 * @author watson email: watson.wu@hotmail.com
 * @date 2009-9-4
 */
public class TableDefinition {

	private List<CRUDField> crud;
	private List<FieldMetadata> fields;
	
	public TableDefinition(List<FieldMetadata> fields) {
		super();
		this.fields = fields;
		this.crud = DAOUtil.getCRUDDefinition(this.fields);
	}

	public TableDefinition(List<CRUDField> crud, boolean flag) {
		super();
		this.crud = crud;
		this.fields = DAOUtil.getFieldMetadata(this.crud);
	}

	public TableDefinition(List<CRUDField> crud, List<FieldMetadata> fields) {
		this.crud = crud;
		this.fields = fields;
	}

	public List<CRUDField> getCrud() {
		return crud;
	}

	public void setCrud(List<CRUDField> crud) {
		this.crud = crud;
	}

	public List<FieldMetadata> getFields() {
		return fields;
	}

	public void setFields(List<FieldMetadata> fields) {
		this.fields = fields;
	}
	
	public List<String> getDisplayableList() {
		ArrayList<String> list = new ArrayList<String>();
		for(FieldMetadata field:fields)
			if(field.isDisplayable())
				list.add(field.getColumnName());
		return list;
	}

	public KeyValueList getDisplayableNameAndTypeName() {
		KeyValueList map = new KeyValueList();
		for(FieldMetadata field:fields) {
			if(field.isDisplayable()) {
				map.add(new KeyValue(field.getColumnName(), field.getColumnTypeName()));
			}
		}
		return map;
	}

	public KeyValueList getDisplayableMapClass() {
		KeyValueList map = new KeyValueList();
		for(FieldMetadata field:fields) {
			if(field.isDisplayable()) {
				map.add(new KeyValue(field.getColumnName(), SQLTypeParserUtil.map2JavaType(field.getColumnTypeName())));
			}
		}
		return map;
	}

	public KeyValueList getSearchableMap() {
		KeyValueList map = new KeyValueList();
		for(FieldMetadata field:fields) {
			if(field.isSearchable())
				map.add(new KeyValue(field.getColumnName(), field.getColumnTypeName()));
		}
		return map;
	}

	public KeyValueList getAllFieldsMapTypeName() {
		KeyValueList map = new KeyValueList();
		for(FieldMetadata field:fields) {
			map.add(new KeyValue(field.getColumnName(), field.getColumnTypeName()));
		}
		return map;
	}

	public String getPk() {
		for(FieldMetadata field:fields) {
			if(field.isPrimaried())
				return field.getColumnName();
		}
		return null;
	}

	public String getPKType() {
		for(FieldMetadata field:fields) {
			if(field.isPrimaried())
				return field.getColumnTypeName();
		}
		return null;
	}

	public String getColumnTypeName(String column) {
		for(FieldMetadata field:fields) {
			if(field.getColumnName().equalsIgnoreCase(column))
				return field.getColumnTypeName();
		}
		return null;
	}

}
